package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarCondition;
import com.feicheng.lanniao.entity.CarRentalPrice;
import com.feicheng.lanniao.entity.CarSellPrice;
import com.feicheng.lanniao.entity.Mileage;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.CarMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.*;
import com.feicheng.lanniao.service.ICarService;
import com.feicheng.lanniao.service.ICarConditionService;
import com.feicheng.lanniao.service.IMileageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

	@Autowired
	private ICarConditionService iCarConditionService;
	@Autowired
	private IMileageService IMileageService;
	@Autowired
	private ICarSellPriceService iCarSellPriceService;
	@Autowired
	private ICarRentalPriceService iCarRentalPriceService;


	@Override
	public ResultInfo carList(String searchKey,Integer conditionId, Integer mileageId, Integer brandId, Integer sellPriceId,Integer rentalPriceId ,Integer currentPage,
			Integer pageSize) {

		QueryWrapper<Car> carQueryWapper = new QueryWrapper<Car>();
		//把删除位是true加入查询条件
		carQueryWapper.eq("del_flag", true);
		//判断查询条件searchKey是否为空，不为空则与数据名字关联
		if(StringUtils.hasLength(searchKey)) {
			carQueryWapper.and(i -> i.like("name", searchKey)
			.or().like("color", searchKey)
			.or().like("owner_ship", searchKey)
			.or().like("transmission_case", searchKey)
			.or().like("type", searchKey)
			.or().like("energy", searchKey)
			.or().like("drive", searchKey)
			.or().like("car_condition", searchKey)
			.or().like("mileage", searchKey));

		}
//		System.out.println(conditionId);
		//判断查询条件conditionId是否为空，不为空则与数据名字关联
		if (null !=conditionId && conditionId!=0) {
			CarCondition carCondition = iCarConditionService.getById(conditionId);
			carQueryWapper.between("car_condition",carCondition.getMinCondition(), carCondition.getMaxCondition());
		}

		//判断查询条件mileageId是否为空或0，不为空或0则与数据名字关联
		if (null !=mileageId && mileageId!=0) {

			Mileage mileage = IMileageService.getById(mileageId);
			carQueryWapper.between("mileage", mileage.getMinMileage(), mileage.getMaxMilleage());
		}

		//判断查询条件brandId是否为空或0，不为空或0则与数据名字关联
		if(null !=brandId && brandId!=0) {
			carQueryWapper.eq("brand_id", brandId);
		}
		//判断查询条件卖车价格是否为空或0，不为空或0则与数据名字关联
		if(null !=sellPriceId && sellPriceId!=0) {

			CarSellPrice carSellPrice = iCarSellPriceService.getById(sellPriceId);
			carQueryWapper.between("sell_price", carSellPrice.getMinPrice(), carSellPrice.getMaxPrice());
		}
		//判断查询条件租车价格是否为空，不为空则与数据名字关联
		if(null !=rentalPriceId && rentalPriceId!=0) {

			CarRentalPrice carRentalPrice = iCarRentalPriceService.getById(rentalPriceId);
			carQueryWapper.between("rental_price", carRentalPrice.getMinPrice(), carRentalPrice.getMaxPrice());
			}


		IPage<Car> pageInfo = new Page<Car>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, carQueryWapper));

}


	@Override
	public ResultInfo getCarinfo() {
		// TODO Auto-generated method stub
		QueryWrapper<Car> carQueryWapper = new QueryWrapper<Car>();
		//把删除位是true加入查询条件
		carQueryWapper.eq("del_flag", true);
		//返回查询结果
		return ResultInfo.success(this.list(carQueryWapper));
	}
	@Override
	public ResultInfo deleteCarById(Integer carId) {

		//如果carId为空或0，抛出异常
		if(null==carId || 0 ==carId) {
			throw new SystemException("carId为空");
		}
		// 构建查询条件
		QueryWrapper<Car> carQueryWapper = new QueryWrapper<Car>();
		// 把主键加入查询条件
		carQueryWapper.eq("car_id", carId);
		 // 根据主键查询出一条数据，用一个对象接收
		Car car = this.getOne(carQueryWapper);
		// 设置删除位值
		car.setDelFlag(false);
		// 修改车辆
		Boolean b = this.updateById(car);
		// 返回修改结果
		if (!b) {
			throw new SystemException("删除失败");
		}
		return ResultInfo.success();
	}

	@Override
	public ResultInfo updateCar(Car car) {

		// 判断车辆ID是否存在,若不存在抛出异常
		if (null == this.getById(car.getCarId())) {
			throw new SystemException("车辆ID缺失,无法更新");
		}
		// 修改车辆
		this.updateById(car);
		// 返回修改结果
		return ResultInfo.success();
	}

	@Override
	public ResultInfo selectCarById(Integer carId) {


		return ResultInfo.success(this.getById(carId));
	}
	@Override
	public ResultInfo addCarInfo(Car car) {

		// 判断车辆ID是否存在,若存在则抛出异常
		if (null != this.getById(car.getCarId())) {
			throw new SystemException("车辆ID已存在，无法添加");
		}

		if(0 == car.getBrandId() || null==car.getBrandId()) {
			throw new SystemException("品牌不能为空") ;
		};
		if(0 == car.getMileage()|| null == car.getMileage()) {
			throw new SystemException("公里数不能为空") ;
		};

		if( null == car.getType()|| "" == car.getType()) {
			throw new SystemException("车型不能为空") ;
		};

//		if( null == car.getName()|| "" == car.getName()) {
//			throw new SystemException("车名不能为空") ;
//		};


		if(car.getCarCondition() == null){
			throw new SystemException("车龄不能为空") ;
		}


		if(null == car.getCarBuyTime() || "" == car.getCarBuyTime()) {
			throw new SystemException("买车时间不能为空") ;
		};


		car.setDelFlag(true);

		this.save(car);

		return ResultInfo.success();
	}

}
